package org.arain.baseii.connect;

import java.util.Date;
import java.util.List;

import org.arain.baseii.exception.UseException;
import org.arain.baseii.mapper.MessageRecordMapper;
import org.arain.baseii.mapper.SysUserMapper;
import org.arain.baseii.pojo.MessageRecord;
import org.arain.baseii.pojo.MessageRecordExample;
import org.arain.baseii.pojo.SysUser;
import org.arain.baseii.pojo.SysUserExample;
import org.arain.baseii.system.user.SysUserDto;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 
* <p>Title:ConnectServiceImpl </p>
* @author Arain
* @date2017年10月26日
 */
@Service
@Transactional
public class ConnectServiceImpl implements ConnectService{
	@Autowired
	private MessageRecordMapper messageRecordMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Override
	public ArainResult save_01(ConnectDto dto) {
		dto.setSender(AppUtils.getUserName());
		dto.setState("0");
		MessageRecord record = new MessageRecord();
		AppUtils.copyProperties(record, dto);
		record.setTime(new Date());
		messageRecordMapper.insert(record);
		return ArainResult.ok(record.getId());
	}
	@Override
	public Integer load_unread(String me,String username) {
		MessageRecordExample example = new MessageRecordExample();
		example.createCriteria().andSenderEqualTo(username).andStateEqualTo((long) 0).andReceiverEqualTo(me);
		List<MessageRecord> list = messageRecordMapper.selectByExample(example);
		return list.size();
	}
	@Override
	public ArainResult load_talk(ConnectDto dto) {
		MessageRecordExample example = new MessageRecordExample();
		example.createCriteria().andReceiverEqualTo(AppUtils.getUserName()).andStateEqualTo((long) 0).andSenderEqualTo(dto.getSender());
		List<MessageRecord> list_unread = messageRecordMapper.selectByExample(example);
		for (MessageRecord messageRecord : list_unread) {
			messageRecord.setState((long) 1);
			messageRecordMapper.updateByPrimaryKey(messageRecord);
		}
		
		JSONArray array = new JSONArray();
		List<MessageRecord> list = messageRecordMapper.selectTalk(AppUtils.getUserName(), dto.getSender());
		for (MessageRecord messageRecord : list) {
			JSONObject object = new JSONObject();
			object.put("time", TimeUtil.format(messageRecord.getTime()));
			object.put("send", messageRecord.getSender());
			object.put("msg", messageRecord.getMsg());
			if(messageRecord.getSender().equals(AppUtils.getUserName())) {
				object.put("who", "#6BC30D");
			} else {
				object.put("who", "red");
			}
			array.add(object);
		}
		return ArainResult.ok(array);
	}
	@Override
	public ArainResult search_user(SysUserDto dto){
		if(AppUtils.getUserName().equalsIgnoreCase(dto.getUsername())) {
			throw new UseException("不允许搜索自己！");
		}
		SysUserExample example = new SysUserExample();
		example.createCriteria().andEnabledEqualTo((short) 1).andUsernameEqualTo(dto.getUsername());
		List<SysUser> list = sysUserMapper.selectByExample(example);
		if(list!=null && list.size()>0) {
			JSONObject object = new JSONObject();
			object.put("id", list.get(0).getId());
			object.put("username", list.get(0).getUsername());
			return ArainResult.ok(object);
		}
		throw new UseException("没有此用户！");
	}

}
